package com.mmall.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mmall.common.ServerResponse;
import com.mmall.dao.CategoryMapper;
import com.mmall.pojo.Category;
import com.mmall.service.ICategoryService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Set;

@Service("iCategoryService")
public class CategoryServicempl implements ICategoryService {

    Logger logger= LoggerFactory.getLogger(CategoryServicempl.class);
    @Autowired
    CategoryMapper categoryMapper;

    public ServerResponse addCategory(String categoryName, Integer parentId) {

        if (parentId == null || StringUtils.isBlank(categoryName)) {
            return ServerResponse.createByErrorMessage("添加商品品类失败,参数错误");
        }
        Category category = new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
        category.setStatus(true);//刚添加的商品品类状态是可用的
        int rowCount = categoryMapper.insert(category);
        if (rowCount > 0) {
            return ServerResponse.createBySuccessMessage("添加商品品类成功");
        }
        return ServerResponse.createByErrorMessage("添加商品品类失败");
    }

    public ServerResponse updateCatrgory(String categoryName,Integer categoryId){
        if (categoryId == null || StringUtils.isBlank(categoryName)) {
            return ServerResponse.createByErrorMessage("更新商品品类名字失败,参数错误");
        }
        Category category = new Category();
        category.setId(categoryId);
        category.setName(categoryName);
        int rowCount=categoryMapper.updateByPrimaryKeySelective(category);
        if (rowCount>0){
            return ServerResponse.createBySuccessMessage("更新商品品类名字成功");
        }
        return ServerResponse.createByErrorMessage("更新商品品类名字失败");
    }

    public ServerResponse<List<Category>> getChildrenParallelCategory(Integer categoryId){

        List<Category> categories=categoryMapper.selectCurrentChildrenCategoryId(categoryId);
        if (CollectionUtils.isEmpty(categories)){
            logger.info("未找到当前产品的子分类");
        }
        return ServerResponse.createBySuccess(categories);
    }

    /**
     * 递归查询本节点id和孩子节点的id
     * @param categoryId
     * @return
     */
    public ServerResponse<List<Integer>> selectCategoryAndChildById(Integer categoryId){

        Set<Category> categorySet= Sets.newHashSet();

        findChildCategory(categorySet,categoryId);
        List<Integer> categoryList= Lists.newArrayList();
        if (categoryId!=null){
            for (Category category:categorySet){
                categoryList.add(category.getId());
            }
        }
        return ServerResponse.createBySuccess(categoryList);
    }


    public Set<Category> findChildCategory(Set<Category> categorySet,Integer categoryId){

        Category category=categoryMapper.selectByPrimaryKey(categoryId);
        if (category!=null){
            categorySet.add(category);
        }
        List<Category> listCategory=categoryMapper.selectCurrentChildrenCategoryId(categoryId);
        for (Category c: listCategory){
            findChildCategory(categorySet,c.getId());
        }

        return categorySet;
    }

}
